
$(function () {

    //初始化插件
    $('#myform').validate({
        //验证规则
        rules: {
            user: {
                required: true,
                isUser: true
            },
            pwd: {
                required: true,
                isPwd: true
            },
            checkPwd: {
                equalTo:"#pwd"
            },
            phone: {
                required: true,
                isPhone: true
            }
        },
        //提示信息
        messages: {
            user: {
                required: '请输入用户名',   
            },
            pwd: {
                required: '请输入密码',
            },
            checkPwd: {
                equalTo:"两次密码不一致"
            },
            phone: {
                required: '请输入手机号',
            }
        }
    });

    /* 
        改三处:
            1. 改方法名: isZipCode === isUser
            2. 改规则: /^[0-9]{6}$/ === /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/
            3. 改提示信息:

        jQuery.validator.addMethod("isZipCode", function (value, element) {
            var tel = /^[0-9]{6}$/;
            return this.optional(element) || (tel.test(value));
        }, "请正确填写您的邮政编码");
    */
    // 用户名验证   
    jQuery.validator.addMethod("isUser", function (value, element) {
        var tel = /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/;//字母开头，允许5-16字节，允许字母数字下划线
        return this.optional(element) || (tel.test(value));
    }, "字母开头，允许5-16字节，允许字母数字下划线");


    // 密码验证   
    jQuery.validator.addMethod("isPwd", function (value, element) {
        var tel = /^[a-zA-Z]\w{5,17}$/;//字母开头，允许6-18字节，允许字母数字下划线
        return this.optional(element) || (tel.test(value));
    }, "字母开头，允许6-18字节，允许字母数字下划线");


    // 手机号验证   
    jQuery.validator.addMethod("isPhone", function (value, element) {
        var tel = /^1[3-9]\d{9}$/;//字母开头，允许6-18字节，允许字母数字下划线
        return this.optional(element) || (tel.test(value));
    }, "请输入正确的手机号");

}) 
